Ext.define('SKJF.controller.MSTACCOUNT',{
	extend: 'Ext.app.Controller',
	views: ['MASTER.v_mstaccount','MASTER.v_mstaccount_form'],
	models: ['m_mstaccount'],
	stores: ['s_mstaccount'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Listmstaccount',
		selector: 'Listmstaccount'
	}, {
		ref: 'v_mstaccount_form',
		selector: 'v_mstaccount_form'
	}, {
		ref: 'Savemstaccount',
		selector: 'v_mstaccount_form #save'
	}, {
		ref: 'Createmstaccount',
		selector: 'v_mstaccount_form #create'
	}, {
		ref: 'MSTACCOUNT',
		selector: 'MSTACCOUNT'
	}],


	init: function(){
		this.control({
			'MSTACCOUNT': {
				'afterrender': this.mstaccountAfterRender
			},
			'v_mstaccount_form': {
				'afterlayout': this.mstaccountAfterLayout
			},
			'Listmstaccount': {
				'selectionchange': this.enableDelete,
				'itemdblclick': this.updateListmstaccount
			},
			'Listmstaccount button[action=create]': {
				click: this.createRecord
			},
			'Listmstaccount button[action=delete]': {
				click: this.deleteRecord
			},
			'Listmstaccount button[action=print]': {
				click: this.printRecords
			},
			'v_mstaccount_form button[action=save]': {
				click: this.saveV_mstaccount_form
			},
			'v_mstaccount_form button[action=create]': {
				click: this.saveV_mstaccount_form
			},
			'v_mstaccount_form button[action=cancel]': {
				click: this.cancelV_mstaccount_form
			}
		});
	},
	
	mstaccountAfterRender: function(){
		var mstaccountStore = this.getListmstaccount().getStore();
		mstaccountStore.load();
	},
	
	mstaccountAfterLayout: function(){this.getV_mstaccount_form().down('#KODE_ACCOUNT_field').focus(false, true);
	},
	
	createRecord: function(){
		var getListmstaccount	= this.getListmstaccount();
		var getV_mstaccount_form= this.getV_mstaccount_form(),
			form			= getV_mstaccount_form.getForm();
		var getSavemstaccount	= this.getSavemstaccount();
		var getCreatemstaccount	= this.getCreatemstaccount();
		
		/* grid-panel */
		getListmstaccount.setDisabled(true);
        
		/* form-panel */
		form.reset();
		getV_mstaccount_form.down('#KODE_ACCOUNT_field').setReadOnly(false);
		getSavemstaccount.setDisabled(true);
		getCreatemstaccount.setDisabled(false);
		getV_mstaccount_form.setDisabled(false);
		
		this.getMSTACCOUNT().setActiveTab(getV_mstaccount_form);		
	},
	
	enableDelete: function(dataview, selections){
		this.getListmstaccount().down('#btndelete').setDisabled(!selections.length);
	},
	
	updateListmstaccount: function(me, record, item, index, e){
		var getMSTACCOUNT		= this.getMSTACCOUNT();
		var getListmstaccount	= this.getListmstaccount();
		var getV_mstaccount_form= this.getV_mstaccount_form(),
			form			= getV_mstaccount_form.getForm();
		var getSavemstaccount	= this.getSavemstaccount();
		var getCreatemstaccount	= this.getCreatemstaccount();
		
		getSavemstaccount.setDisabled(false);
		getCreatemstaccount.setDisabled(true);
		getV_mstaccount_form.down('#KODE_ACCOUNT_field').setReadOnly(true);		
		getV_mstaccount_form.loadRecord(record);
		
		getListmstaccount.setDisabled(true);
		getV_mstaccount_form.setDisabled(false);
		getMSTACCOUNT.setActiveTab(getV_mstaccount_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getListmstaccount().getStore();
		var selection = this.getListmstaccount().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "KODE_ACCOUNT" = "'+selection.data.KODE_ACCOUNT+'","ID" = "'+selection.data.ID+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	printRecords: function(){
		var getstore = this.getListmstaccount().getStore();
		var jsonData = Ext.encode(Ext.pluck(getstore.data.items, 'data'));
		
		Ext.Ajax.request({
			method: 'POST',
			url: 'c_mstaccount/printRecords',
			params: {data: jsonData},
			success: function(response){
				var result=eval(response.responseText);
				switch(result){
				case 1:
					win = window.open('./temp/mstaccount.html','mstaccount_list','height=400,width=900,resizable=1,scrollbars=1, menubar=1');
					break;
				default:
					Ext.MessageBox.show({
						title: 'Warning',
						msg: 'Unable to print the grid!',
						buttons: Ext.MessageBox.OK,
						animEl: 'save',
						icon: Ext.MessageBox.WARNING
					});
					break;
				}  
			}
		});
	},
	
	saveV_mstaccount_form: function(){
		var getMSTACCOUNT		= this.getMSTACCOUNT();
		var getListmstaccount 	= this.getListmstaccount();
		var getV_mstaccount_form= this.getV_mstaccount_form(),
			form			= getV_mstaccount_form.getForm(),
			values			= getV_mstaccount_form.getValues();
		var store 			= this.getStore('s_mstaccount');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_mstaccount/save',
				params: {data: jsonData},
				success: function(response){
					store.reload({
						callback: function(){
							var newRecordIndex = store.findBy(
								function(record, id) {
									if (record.get('KODE_ACCOUNT') === values.KODE_ACCOUNT) {
										return true;
									}
									return false;
								}
							);
							/* getListmstaccount.getView().select(recordIndex); */
							getListmstaccount.getSelectionModel().select(newRecordIndex);
						}
					});
					
					getV_mstaccount_form.setDisabled(true);
					getListmstaccount.setDisabled(false);
					getMSTACCOUNT.setActiveTab(getListmstaccount);
				}
			});
		}
	},
	
	createV_mstaccount_form: function(){
		var getMSTACCOUNT		= this.getMSTACCOUNT();
		var getListmstaccount 	= this.getListmstaccount();
		var getV_mstaccount_form= this.getV_mstaccount_form(),
			form			= getV_mstaccount_form.getForm(),
			values			= getV_mstaccount_form.getValues();
		var store 			= this.getStore('s_mstaccount');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_mstaccount/save',
				params: {data: jsonData},
				success: function(response){
					store.reload();
					
					getV_mstaccount_form.setDisabled(true);
					getListmstaccount.setDisabled(false);
					getMSTACCOUNT.setActiveTab(getListmstaccount);
				}
			});
		}
	},
	
	cancelV_mstaccount_form: function(){
		var getMSTACCOUNT		= this.getMSTACCOUNT();
		var getListmstaccount	= this.getListmstaccount();
		var getV_mstaccount_form= this.getV_mstaccount_form(),
			form			= getV_mstaccount_form.getForm();
			
		form.reset();
		getV_mstaccount_form.setDisabled(true);
		getListmstaccount.setDisabled(false);
		getMSTACCOUNT.setActiveTab(getListmstaccount);
	}
	
});